﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

/// <summary>
/// Summary description for AdminData
/// </summary>
public class AdminData
{
 

    public AdminData()
    {
        System.Data.SqlClient.SqlConnection.ClearAllPools();
    }


    SqlCommand cm = null;
    SqlDataReader rd = null;
    DataSet ds = null;
    SqlDataAdapter da = null;
    MD5 md5 = null;

    private static Random _random = new Random();

    public static T NumToEnum<T>(int number)
    {
        return (T)Enum.ToObject(typeof(T), number);
    }

    public static SqlConnection ConnectionOpen()
    {
        SqlConnection cn = null;
        cn = new SqlConnection();
        cn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringDev"].ConnectionString;
        cn.Open();
        return cn;
    }

    public void Addplan(int PlanDurationID,int PlanUserRangeID,int Cost)
    {

        using (SqlCommand cmd = new SqlCommand("Add_Plan"))
        {
            using (SqlConnection cn = ConnectionOpen())
            {
                cmd.Connection = cn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandTimeout = 0;

                SqlParameter spPlanDurationID = new SqlParameter("@PlanDurationID", SqlDbType.Int);
                spPlanDurationID.Value = PlanDurationID;
                cmd.Parameters.Add(spPlanDurationID);

                SqlParameter spPlanUserRangeID = new SqlParameter("@PlanUserRangeID", SqlDbType.Int);
                spPlanUserRangeID.Value = PlanUserRangeID;
                cmd.Parameters.Add(spPlanUserRangeID);

                SqlParameter spCost = new SqlParameter("@Cost", SqlDbType.Int);
                spCost.Value = Cost;
                cmd.Parameters.Add(spCost);

                cmd.ExecuteNonQuery();
            }
        } 
    }


    public void Add_PlanDuration(string PlanName, int Duration)
    {

        using (SqlCommand cmd = new SqlCommand("Add_PlanDuration"))
        {
            using (SqlConnection cn = ConnectionOpen())
            {
                cmd.Connection = cn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandTimeout = 0;

                SqlParameter spPlanName = new SqlParameter("@PlanName", SqlDbType.VarChar);
                spPlanName.Value = PlanName;
                cmd.Parameters.Add(spPlanName);

                SqlParameter spDuration = new SqlParameter("@Duration", SqlDbType.Int);
                spDuration.Value = Duration;
                cmd.Parameters.Add(spDuration);
                 

                cmd.ExecuteNonQuery();
            }
        }
    }

    public void Add_PlanUserRange(string LRange,string URange,string Display, int PlanOrder)
    {

        using (SqlCommand cmd = new SqlCommand("Add_PlanUserRange"))
        {
            using (SqlConnection cn = ConnectionOpen())
            {
                cmd.Connection = cn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandTimeout = 0;

                SqlParameter spLRange = new SqlParameter("@LRange", SqlDbType.VarChar);
                spLRange.Value = LRange;
                cmd.Parameters.Add(spLRange);

                SqlParameter spURange = new SqlParameter("@URange", SqlDbType.VarChar);
                spURange.Value = URange;
                cmd.Parameters.Add(spURange);


                SqlParameter spDisplay = new SqlParameter("@Display", SqlDbType.VarChar);
                spDisplay.Value = Display;
                cmd.Parameters.Add(spDisplay);

                SqlParameter spPlanOrder = new SqlParameter("@PlanOrder", SqlDbType.Int);
                spPlanOrder.Value = PlanOrder;
                cmd.Parameters.Add(spPlanOrder);


                cmd.ExecuteNonQuery();
            }
        }
    }


    public void Activate_Plan(int PlanUserRangeID, int PlanDurationID, int PlanID, int IsActivate)
    {

        using (SqlCommand cmd = new SqlCommand("Activate_Plan"))
        {
            using (SqlConnection cn = ConnectionOpen())
            {
                cmd.Connection = cn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandTimeout = 0;

                SqlParameter spPlanPlanIDID = new SqlParameter("@PlanDurationID", SqlDbType.Int);
                spPlanPlanIDID.Value = PlanDurationID;
                cmd.Parameters.Add(spPlanPlanIDID);

                SqlParameter spPlanUserRangeID = new SqlParameter("@PlanUserRangeID", SqlDbType.Int);
                spPlanUserRangeID.Value = PlanUserRangeID;
                cmd.Parameters.Add(spPlanUserRangeID);

                SqlParameter spCost = new SqlParameter("@PlanID", SqlDbType.Int);
                spCost.Value = PlanID;
                cmd.Parameters.Add(spCost);

                SqlParameter spIsActivate = new SqlParameter("@IsActivate", SqlDbType.Int);
                spIsActivate.Value = IsActivate;
                cmd.Parameters.Add(spIsActivate);

                cmd.ExecuteNonQuery();
            }
        }
    }
    public void DeActivate_Plan(int PlanDurationID, int PlanUserRangeID, int Cost)
    {

        using (SqlCommand cmd = new SqlCommand("Deactivate_Plan"))
        {
            using (SqlConnection cn = ConnectionOpen())
            {
                cmd.Connection = cn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandTimeout = 0;

                SqlParameter spPlanPlanIDID = new SqlParameter("@PlanDurationID", SqlDbType.Int);
                spPlanPlanIDID.Value = PlanDurationID;
                cmd.Parameters.Add(spPlanPlanIDID);

                SqlParameter spPlanUserRangeID = new SqlParameter("@PlanUserRangeID", SqlDbType.Int);
                spPlanUserRangeID.Value = PlanUserRangeID;
                cmd.Parameters.Add(spPlanUserRangeID);

                SqlParameter spCost = new SqlParameter("@Cost", SqlDbType.Int);
                spCost.Value = Cost;
                cmd.Parameters.Add(spCost);

                cmd.ExecuteNonQuery();
            }
        }
    }

    public DataSet Select_Plan(string TableName)
    {
        DataSet ds = new DataSet();
        using (SqlCommand cmd = new SqlCommand("Select_Plan"))
        {
            using (SqlConnection cn = ConnectionOpen())
            {
                cmd.Connection = cn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandTimeout = 0;

                SqlParameter spTableName = new SqlParameter("@TableName", SqlDbType.VarChar);
                spTableName.Value = TableName;
                cmd.Parameters.Add(spTableName);


                using (da = new SqlDataAdapter(cmd))
                {
                    da.Fill(ds);
                }
            }
        }
        return ds;
    }

}